{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "14beecb1-9278-4cca-ae73-9b588f485004",
   "metadata": {},
   "source": [
    "## 复习\n",
    "\n",
    "1. 什么是二维列表\n",
    "   - 列表中嵌套列表\n",
    "2. 如何操作二维列表\n",
    "   - 通过索引访问元素\n",
    "   - 遍历列表  \n",
    "3. 三维列表就是列表套列表套列表\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "171ad43e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d243ebd5-7d9b-45c5-a2b7-fd241b6e74fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "#一维列表\n",
    "li = [1,2,3]\n",
    "\n",
    "#定义一个二维列表\n",
    "lis = [[1,2,3],\n",
    "       [4,5,6],\n",
    "       [7,8,9]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "47d94fef-e46e-4218-ae76-1b95a4cdda9d",
   "metadata": {},
   "outputs": [],
   "source": [
    "#遍历二维数组\n",
    "for i in range(len(lis)):\n",
    "    #遍历一维列表\n",
    "    for j in range(len(lis[i])):\n",
    "        print(lis[i][j])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "75d19a1a-3fad-4313-b6cb-dc5a4b8a3533",
   "metadata": {},
   "outputs": [],
   "source": [
    "#直接遍历元素\n",
    "for i in lis:\n",
    "    \n",
    "    for j in i:\n",
    "        print(j)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e8f6fa6-4015-448c-9176-c4129be63b75",
   "metadata": {},
   "source": [
    "## 字典\n",
    "\n",
    "### 什么是字典\n",
    "> 键值对关系的,可以一一对应的key和value\n",
    "\n",
    "### 如何定义一个字典\n",
    "三个重要字符:\n",
    "1. `{}` (大/花括号) 代表开始和结束 `{k:v, k1:v1}`\n",
    "2. `,` (逗号) 用来分割 `k:v, k1:v1`\n",
    "3. `:` (冒号) 区分key和value `key:value`\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "60b0c5b9-c8f9-48d4-9586-ec19f393a6a3",
   "metadata": {},
   "outputs": [],
   "source": [
    "#定义一个字典\n",
    "\n",
    "#键不可以重复,如果重复,就覆盖了\n",
    "phone_num = {'mom':18723912763,'mom':1203971023790912,'dad':8899776655}\n",
    "\n",
    "print(phone_num)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0bb7ed70-e1de-462e-b61c-91c33176653c",
   "metadata": {},
   "source": [
    "## 怎么操作字典\n",
    "> 我们可以将字典当作一种特别的列表\n",
    "1. 增加\n",
    "   ```python\n",
    "   # 无中生有大法\n",
    "   dict[新增的key] = 新增的值\n",
    "   ```\n",
    "2. 删除\n",
    "   ```python\n",
    "   del dict[key]\n",
    "   ```\n",
    "3. 修改\n",
    "   - 直接修改\n",
    "4. 查询\n",
    "   - `dict[key]`\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "56431da1",
   "metadata": {},
   "outputs": [],
   "source": [
    "#查询\n",
    "print(phone_num['mom'])\n",
    "\n",
    "print(phone_num['dad'])\n",
    "\n",
    "#print(phone_num['bro'])不可以查询不存在的键值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6f81dd08-67c7-4a0c-a304-ca729dd82772",
   "metadata": {},
   "outputs": [],
   "source": [
    "#查询\n",
    "print(phone_num['mom'])\n",
    "\n",
    "print(phone_num['dad'])\n",
    "\n",
    "#print(phone_num['bro'])不可以查询不存在的键值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ac9ee807-4a3e-479f-bb87-5416735311fb",
   "metadata": {},
   "outputs": [],
   "source": [
    "#增加,无中生有大法\n",
    "phone_num['bro'] = 666\n",
    "\n",
    "print(phone_num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11612343-0117-43b9-ab3e-f1d3e6985cee",
   "metadata": {},
   "outputs": [],
   "source": [
    "#修改\n",
    "\n",
    "#修改原本就有的值\n",
    "phone_num['bro'] = 666666666\n",
    "\n",
    "#修改一个不存在的值,增加了\n",
    "phone_num['sis'] = 7777777777\n",
    "\n",
    "print(phone_num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "487a7c1e-2961-4328-917d-a3f7a8e4a847",
   "metadata": {},
   "outputs": [],
   "source": [
    "#删除\n",
    "\n",
    "del phone_num['mom']\n",
    "\n",
    "print(phone_num)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17f16d6c-6fcd-44ce-8474-02edaa3d02cc",
   "metadata": {},
   "source": [
    "## 怎么查询\n",
    "\n",
    "### 查询一个\n",
    "> 直接查就行,dict[key]\n",
    ">\n",
    "### 查询全部--遍历字典\n",
    "1. dict.keys() 返回全部的key列表\n",
    "2. dict.values() 返回全部的value列表\n",
    "3. dict.items() 返回全部的(key,value)的列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "33ace1f8-a5e5-41f4-80ae-0ccbaee4ca94",
   "metadata": {},
   "outputs": [],
   "source": [
    "#获取所有的键的信息的列表\n",
    "print(phone_num.keys())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "71533d22-9ae7-4bf3-998a-aecc735115cd",
   "metadata": {},
   "outputs": [],
   "source": [
    "#获取所有值\n",
    "print(phone_num.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "db544dc6-1053-4846-901e-4414bf980c57",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(phone_num.items())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "226ba4d1-1a07-4328-a514-1d295e3a671b",
   "metadata": {},
   "outputs": [],
   "source": [
    "#直接遍历字典,相当于直接遍历key\n",
    "for i in phone_num:\n",
    "    print(i,end=' ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b93aa466-6bb0-4915-bc0a-6cb83cee8422",
   "metadata": {},
   "outputs": [],
   "source": [
    "#遍历所有值\n",
    "for i in phone_num.values():\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a3ebe6b7-bc16-4d33-87ab-9d4411783685",
   "metadata": {},
   "outputs": [],
   "source": [
    "#遍历所有的key和value\n",
    "for k,v in phone_num.items():\n",
    "    print(k,'的手机号是',v,sep='')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ac49a42-b86e-4e9e-9231-7285ec632f26",
   "metadata": {},
   "source": [
    "## 课堂练习\n",
    "\n",
    "假设一个故事,让计算机来讲\n",
    "> 故事5元素: 时间,地点,人物,什么事情,为什么"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "63d9c393-d94e-41ef-9619-5aa6eae4038c",
   "metadata": {},
   "outputs": [],
   "source": [
    "#随机库\n",
    "import random\n",
    "\n",
    "#一种设定方式\n",
    "pig = {'when':'在一个晴朗的午后','where':'在一个草坪上','who':'小猪佩奇','what':'在踩泥坑','why':'因为它是一头猪'}\n",
    "lpig = {'when':'在一个晴朗的午后','where':'在一个草坪上','who':'小猪乔治','what':'也在踩泥坑','why':'因为它也是一头猪'}\n",
    "\n",
    "#另一种设定方式\n",
    "#1. 定义一个空角色\n",
    "opig = {}\n",
    "opig['when'] = '在一个晴朗的午后'\n",
    "opig['where'] = '在草坪'\n",
    "opig['who']='老猪爸爸'\n",
    "opig['what'] = '在跳水'\n",
    "\n",
    "#再一种设定方式\n",
    "#直接用别人的信息\n",
    "pig_mom = {}\n",
    "for k,v in pig.items():\n",
    "    pig_mom[k] = v\n",
    "pig_mom['who'] = '猪妈妈'\n",
    "\n",
    "#把角色都装在一个栅栏里\n",
    "all = [pig,lpig,opig,pig_mom]\n",
    "\n",
    "#随机选出一个主角\n",
    "主角 = all[random.randint(0,len(all)-1)] #列表的长度-1\n",
    "\n",
    "#开始讲故事\n",
    "for i in 主角.values():\n",
    "    print(i,end = ' ' )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "30e6ba30-79dc-462f-921c-45c7a591177f",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(pig_mom)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "29bfb8db-28d8-4f7b-8826-7eb30cc965d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "\n",
    "student = {'when':'一天凌晨三点','where':'在草地上','who':'霸王龙小分队','what':'吃炸鸡','why':'因为他拉稀啦'}\n",
    "\n",
    "#创建方式2\n",
    "student2 = {}\n",
    "student2['when'] = '上午吃饭的时候'\n",
    "student2['where'] = '在绿宝楼下'\n",
    "student2['who'] = '不知道哪个同学'\n",
    "student2['what'] = '买了瓶饮料'\n",
    "student2['why'] = '因为面太辣了'\n",
    "\n",
    "#创建方式3\n",
    "student3 = {}\n",
    "for k,v in student.items():\n",
    "    student3[k] = v\n",
    "#给他改名字\n",
    "student3['who'] = '王小明'\n",
    "\n",
    "#把所有的学生放在一起\n",
    "all = [student,student2,student3]\n",
    "\n",
    "one = all[random.randint(0,2)]\n",
    "\n",
    "\n",
    "for i in student.values():\n",
    "    print(i,end = ' ')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
